package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.dao.ExceptionLogMapper;
import com.itheima.dao.SysLogMapper;
import com.itheima.pojo.ExceptionLog;
import com.itheima.pojo.SysLog;
import com.itheima.pojo.User;
import com.itheima.service.LogService;
import com.itheima.utils.StringUtils;
import com.itheima.vo.ExceptionLogVo;
import com.itheima.vo.SysLogVo;
import org.bouncycastle.cms.PasswordRecipientId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @Author: Felix(李鑫)
 * @Date: 2020/9/1 18:19
 * @Description:
 */

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private ExceptionLogMapper exceptionLogMapper;


    /**
     * 分页获取系统日志信息
     * @param sysLogVo
     * @return
     */
    @Override
    public List<SysLog> getLogList(SysLogVo sysLogVo) {
        if (sysLogVo.getCurrentPage()!=null|| sysLogVo.getPageSize()!= null) {
            PageHelper.startPage(sysLogVo.getCurrentPage(), sysLogVo.getPageSize());
        }
        Example example = new Example(SysLog.class);
        Example.Criteria criteria = example.createCriteria();
        String keyword = sysLogVo.getKeyword();
        if(keyword!=null){
            criteria.orLike("ip","%"+keyword+"%")
                    .orLike("user_name","%"+keyword+"%")
                    .orLike("ip_source","%"+keyword+"%")
                    .orLike("admin_uid","%"+keyword+"%")
                    .orLike("operation","%"+keyword+"%");
        }
        List<SysLog> sysLogList = sysLogMapper.selectByExample(example);
        return sysLogList;
    }

    /**
     * 分页获取系统异常信息
     * @param exceptionLogVo
     * @return
     */
    @Override
    public List<ExceptionLog> getExceptionList(ExceptionLogVo exceptionLogVo) {
        if (exceptionLogVo.getCurrentPage()!=null|| exceptionLogVo.getPageSize()!= null) {
            PageHelper.startPage(exceptionLogVo.getCurrentPage(), exceptionLogVo.getPageSize());
        }
        Example example = new Example(ExceptionLog.class);
        Example.Criteria criteria = example.createCriteria();
        String keyword = exceptionLogVo.getKeyword();
        if(keyword!=null){
            criteria.orLike("exception_json","%"+keyword+"%")
                    .orLike("exception_message","%"+keyword+"%")
                    .orLike("ip","%"+keyword+"%")
                    .orLike("operation","%"+keyword+"%");
        }
        List<ExceptionLog> exceptionLogs = exceptionLogMapper.selectByExample(example);

        return exceptionLogs;
    }
}
